package cn.bincker.mybatis.encrypt.type;

import org.apache.ibatis.type.*;

import java.sql.*;

public class NoEncryptEncryptParamTypeHandler extends BaseTypeHandler<EncryptParam<?>> {
    private final TypeHandlerRegistry typeHandlerRegistry;

    public NoEncryptEncryptParamTypeHandler(TypeHandlerRegistry typeHandlerRegistry) {
        this.typeHandlerRegistry = typeHandlerRegistry;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, EncryptParam<?> parameter, JdbcType jdbcType) throws SQLException {
        var value = parameter.getValue();
        //noinspection unchecked
        var typeHandler = (TypeHandler<Object>) typeHandlerRegistry.getTypeHandler(value.getClass());
        if (typeHandler == null) throw new TypeException("not found type handler: type=" + value.getClass());
        typeHandler.setParameter(ps,i, value, jdbcType);
    }

    @Override
    public EncryptParam<?> getNullableResult(ResultSet rs, String columnName) {
        throw new UnsupportedOperationException();
    }

    @Override
    public EncryptParam<?> getNullableResult(ResultSet rs, int columnIndex) {
        throw new UnsupportedOperationException();
    }

    @Override
    public EncryptParam<?> getNullableResult(CallableStatement cs, int columnIndex) {
        throw new UnsupportedOperationException();
    }
}
